//do ...\RunCLogit_046_EEGap_cty_demo_v08232018_2008_2012_c3.do sub_sample inc version seed

//do /Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Code_JPEmicro/RunCLogit_046_EEGap_cty_demo_v08232018_2008_2012_c3.do 44000 16 v11_11022017_robustb 1  1  1  1  1  1  1  1 1 1
 

clear all
set more off
set mem 10000m
set matsize 10000
pause on
       
/*
Options:	 44000 16 v11_11022017_robustb 1 
*/


local subsample 		`"`1'"'
local group_id_selected `"`2'"'
local version			`"`3'"'
local set_seed          `"`4'"'
local t_1_0             `"`5'"' 
local t_1_1             `"`6'"' 
local t_1_2             `"`7'"' 
local t_1_3             `"`8'"'  
local t_1_4             `"`9'"' 
local t_1_5             `"`10'"' 
local t_1_6             `"`11'"' 
local t_1_7             `"`12'"' 
local t_1_8             `"`13'"' 


global pathresults="/Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Results_Estimation/Clogit_stata"
global censuspath ="/Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Data_JPEmicro"
global pathname   ="/Users/shoude/Dropbox/eegap/EEgap_data_code_heter_SM/Data_JPEmicro/Clogit_stata"

log using  $pathresults/RunCLogit_046_EEGap_demo_v6_2008_2012_c3_paper_robust_08232018_`subsample'_`group_id_selected'_seed_`set_seed'_table_`t_1_0'_`t_1_1'_`t_1_2'_`t_1_3'_`t_1_4'_`t_1_5'_`t_1_6'_`t_1_7'_`t_1_8_replication'.txt, append


use $pathname/lcidemo_046_households_2008_2012_`subsample'_`group_id_selected'_seed_`set_seed'_EstReady_robust3.dta, replace

preserve
	collapse (mean) choice_size, by(hd_id)
	su choice_size
	tab choice_size
restore

sort  state year week
merge  state year week using  $pathname/tax_estar_`subsample'_`group_id_selected'_seed_`set_seed'_EstReady_robust.dta

//sort year week state zip
//merge year week state zip using $pathname\refrigerators\tax_rate_median_estar_week_zip_046_2008_2012

tab _m
keep if _m==3
drop _m

//Create Prices
gen non_estar=-1*s_estar+1 
gen tax=promo_f*(tax_rate1*s_estar+tax_rate0*non_estar)
gen real_price=promo_f+tax

bys pid_id: egen promo_pid=mean(promo_f) 
xtile promo_quartile=promo_pid,nq(4)

tab pcode
recode pcode (1=0) (2=1)
tab pcode
/// store type A,B,O
//tab store 
//xi i.store

gen elec_cost=kwh*p_elec/100
gen elec_cost_county=kwh*pcounty_elec/100
gen rebate_estar=s_estar*amount

gen rebate_utility=s_estar*incentive_utility
gen rebate_cfa=s_estar*incentive_cfa	

egen zip_pid=group(pid_id zipcode)

gen week_num=week
replace week_num=week+52 if year==2009
replace week_num=week+104 if year==2010
replace week_num=week+156 if year==2011


//Distinguish 2008 and 2010
bys pid_id: egen delisted2010_tmp=max(delisted2010)
drop delisted2010
ren delisted2010_tmp delisted2010


//Flag decertified models 
gen delisted=0
bys pid_id: egen mean_estar=mean(s_estar)
replace delisted=1 if mean_estar>0 & mean_estar<1 & delisted2010!=1

gen decertified=s_estar*-1+1
gen Tsince2008=(month-4)*decertified
replace Tsince2008=0 if delisted2010==1
replace Tsince2008=0 if delisted==0
gen Tsince2010=(month-25)*decertified
replace Tsince2010=0 if delisted==1
replace Tsince2010=0 if delisted==0 & delisted2010==0
gen s_estar_2010=delisted2010*s_estar

tab Tsince2008
tab Tsince2010
tab pid_id if delisted2010==1


//Demographics
gen fam_size=adult+children
gen Dfam=(children>0)
egen mean_fam_size=mean(fam_size)
egen mean_age=mean(age)
replace age=mean_age if age==.
replace fam_size=mean_fam_size if fam_size==.
mvencode education,mv(1) over

//gen political_id=cond(political=="R",1,cond(political=="D",2,cond(political=="I",3,cond(political=="V",4,5)) ))
capture gen political_id=cond(political=="R",1,cond(political=="D",2,3))

gen ageXp_elec=age*elec_cost_county 
gen fam_sizeXp_elec=fam_size*elec_cost_county
gen ageXAV=age*AV 
gen ageXreal_price=age*real_price
gen fam_sizeXreal_price=fam_size*real_price
gen fam_sizeXAV=fam_size*AV
gen age_gr=cond(age<=30,1,cond(age>30 & age<=45,2,cond(age>45 & age<=55,3,cond(age>55 & age<=70,4,5))))
gen age_gr2=cond(age<=35,1,cond(age>35 & age<=49,2,cond(age>49 & age<=65,3,4)))
gen fam_size_gr=cond(fam_size==1,1,cond(fam_size==2,2,cond(fam_size==3 | fam_size==4,3,5)))
//xtile age_gr=age,nq(5)
//xtile fam_size_gr=fam_size,nq(5)


//Generate Fixed Effects
gen brand_sht_id=0
replace brand_sht_id=1 if brand_id=="Kenmore_Low" | brand_id=="Kenmore_High" 
replace brand_sht_id=2 if brand_id=="Whirlpool_Low" | brand_id=="Whirlpool_High" 
replace brand_sht_id=3 if brand_id=="Others_Low" | brand_id=="Others_High" 
replace brand_sht_id=4 if brand_id=="GE_Low" | brand_id=="GE_High" 
replace brand_sht_id=5 if brand_id=="Frigidaire_Low" | brand_id=="Frigidaire_High" 

egen state_id=group(state)						
gen state_es=state_id*s_estar						
egen state_year_es=group(state year s_estar)
egen state_promo_brand=group(state promo_quartile brand_sht_id)
							
egen county_id=group(county_utility)
gen county_es=county_id*s_estar	
			
egen brand_week_num=group(brand_sht_id week_num)
replace brand_week_num=0 if brand_sht_id==3
egen brand_month_num=group(brand_sht_id month)
replace brand_month_num=0 if brand_sht_id==3

//Nested logit and Ackerberg and Rysman Congested Logit

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Main Table:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


gen size_id=0
replace size_id=1 if type_id==1 & AV>=12 & AV<23.5 & AV!=.
replace size_id=2 if type_id==1 & AV>=23.5 & AV!=.

replace size_id=1 if type_id==2 & AV>=12 & AV<29.2 & AV!=.
replace size_id=2 if type_id==2 & AV>=29.2 & AV!=.

replace size_id=1 if type_id==3 & AV>=12 & AV<28 & AV!=.
replace size_id=2 if type_id==3 & AV>=28 & AV!=.

gen county_size=county_id*size_id	
gen type_id_2_3=cond(type_id>1,1,0)
gen county_type_2_3=county_id*type_id_2_3


if `t_1_2'==1{    
																		
_eststo t_1_2_`group_id_selected'_seed_`set_seed', title("CLOGIT") : xi: clogit choice real_price elec_cost_county rebate_utility rebate_cfa s_estar i.pid_id ///
					i.type_id*i.income i.type_id*i.education i.type_id|fam_size  i.type_id|age i.type_id*i.political_id ///
					i.income|AV i.education|AV ageXAV fam_sizeXAV i.political_id|AV  ///
					i.county_es , gr(hd_id) iterate(20) vce(cluster zipcode) nonest						

cd $pathresults					
esttab t_1_2_`group_id_selected'_seed_`set_seed'  using Tables_EEGap_CL_stata_2_2008_2012_cty_paper_robust_08232018_`subsample'_`group_id_selected'_seed_`set_seed'.tex, replace b(%10.5f) r2 ar2 pr2 compress booktabs page se margin star scalars(N_clust) mtitles((OLS) (IV)  (IV)) label coeflabels("Price ($P$)" "Energy Star ($D$)"  "Rebate (RebateXD)" "Elec Cost ($C$)") 
	
}					
	
if `t_1_3'==1{   				
_eststo t_1_3_`group_id_selected'_seed_`set_seed', title("CLOGIT") : xi: clogit choice real_price elec_cost_county rebate_utility rebate_cfa s_estar i.pid_id ///
					i.type_id*i.income i.type_id*i.education i.type_id|fam_size  i.type_id|age i.type_id*i.political_id ///
					i.income|AV i.education|AV ageXAV fam_sizeXAV i.political_id|AV  ///
					i.county_es i.brand_week_num, gr(hd_id) iterate(20) vce(cluster zipcode) nonest

cd $pathresults					
esttab t_1_3_`group_id_selected'_seed_`set_seed'  using Tables_EEGap_CL_stata_3_2008_2012_cty_paper_robust_08232018_`subsample'_`group_id_selected'_seed_`set_seed'.tex, replace b(%10.5f) r2 ar2 pr2 compress booktabs page se margin star scalars(N_clust) mtitles((OLS) (IV)  (IV)) label coeflabels("Price ($P$)" "Energy Star ($D$)"  "Rebate (RebateXD)" "Elec Cost ($C$)") 
						
					}		

if `t_1_1'==1{   																							
_eststo t_1_1_`group_id_selected'_seed_`set_seed', title("CLOGIT") : xi: clogit choice real_price elec_cost_county rebate_utility rebate_cfa s_estar i.pid_id ///
					i.type_id*i.income i.type_id*i.education i.type_id|fam_size  i.type_id|age i.type_id*i.political_id ///
					i.income|AV i.education|AV ageXAV fam_sizeXAV i.political_id|AV  ///
					, gr(hd_id) iterate(20) vce(cluster zipcode) nonest

cd $pathresults					
esttab t_1_1_`group_id_selected'_seed_`set_seed'  using Tables_EEGap_CL_stata_1_2008_2012_cty_paper_robust_08232018_`subsample'_`group_id_selected'_seed_`set_seed'.tex, replace b(%10.5f) r2 ar2 pr2 compress booktabs page se margin star scalars(N_clust) mtitles((OLS) (IV)  (IV)) label coeflabels("Price ($P$)" "Energy Star ($D$)"  "Rebate (RebateXD)" "Elec Cost ($C$)") 
						
					}

if `t_1_0'==1{   					
_eststo t_1_0_`group_id_selected'_seed_`set_seed', title("CLOGIT") : xi: clogit choice real_price elec_cost_county rebate_utility rebate_cfa s_estar i.pid_id ///
					, gr(hd_id) iterate(100) vce(cluster zipcode) nonest						

cd $pathresults					
esttab t_1_0_`group_id_selected'_seed_`set_seed'  using Tables_EEGap_CL_stata_0_2008_2012_cty_paper_robust_08232018_`subsample'_`group_id_selected'_seed_`set_seed'.tex, replace b(%10.5f) r2 ar2 pr2 compress booktabs page se margin star scalars(N_clust) mtitles((OLS) (IV)  (IV)) label coeflabels("Price ($P$)" "Energy Star ($D$)"  "Rebate (RebateXD)" "Elec Cost ($C$)") 
					
}																					

if `t_1_4'==1{   				
_eststo t_1_4_`group_id_selected'_seed_`set_seed', title("CLOGIT") : xi: clogit choice real_price elec_cost_county rebate_utility rebate_cfa s_estar i.pid_id ///
					i.type_id*i.income i.type_id*i.education i.type_id|fam_size  i.type_id|age i.type_id*i.political_id ///
					i.income|AV i.education|AV ageXAV fam_sizeXAV i.political_id|AV  ///
					i.county_type_2_3 i.brand_week_num, gr(hd_id) iterate(50) vce(cluster zipcode) nonest

cd $pathresults					
esttab t_1_4_`group_id_selected'_seed_`set_seed'  using Tables_EEGap_CL_stata_4_2008_2012_cty_paper_robust_08232018_`subsample'_`group_id_selected'_seed_`set_seed'.tex, replace b(%10.5f) r2 ar2 pr2 compress booktabs page se margin star scalars(N_clust) mtitles((OLS) (IV)  (IV)) label coeflabels("Price ($P$)" "Energy Star ($D$)"  "Rebate (RebateXD)" "Elec Cost ($C$)") 
						
					}

if `t_1_5'==1{   				
_eststo t_1_5_`group_id_selected'_seed_`set_seed', title("CLOGIT") : xi: clogit choice real_price elec_cost_county rebate_utility rebate_cfa s_estar i.pid_id ///
					i.type_id*i.income i.type_id*i.education i.type_id|fam_size  i.type_id|age i.type_id*i.political_id ///
					i.income|AV i.education|AV ageXAV fam_sizeXAV i.political_id|AV  ///
					i.county_size i.brand_week_num, gr(hd_id) iterate(50) vce(cluster zipcode) nonest

cd $pathresults					
esttab t_1_5_`group_id_selected'_seed_`set_seed'  using Tables_EEGap_CL_stata_5_2008_2012_cty_paper_robust_08232018_`subsample'_`group_id_selected'_seed_`set_seed'.tex, replace b(%10.5f) r2 ar2 pr2 compress booktabs page se margin star scalars(N_clust) mtitles((OLS) (IV)  (IV)) label coeflabels("Price ($P$)" "Energy Star ($D$)"  "Rebate (RebateXD)" "Elec Cost ($C$)") 
						
					}					

if `t_1_6'==1{   				
_eststo t_1_6_`group_id_selected'_seed_`set_seed', title("CLOGIT") : xi: clogit choice real_price elec_cost_county rebate_utility rebate_cfa s_estar i.pid_id ///
					i.type_id*i.income i.type_id*i.education i.type_id|fam_size  i.type_id|age i.type_id*i.political_id ///
					i.income|AV i.education|AV ageXAV fam_sizeXAV i.political_id|AV  ///
					i.county_es i.county_type_2_3 i.brand_week_num, gr(hd_id) iterate(50) vce(cluster zipcode) nonest

cd $pathresults					
esttab t_1_6_`group_id_selected'_seed_`set_seed'  using Tables_EEGap_CL_stata_6_2008_2012_cty_paper_robust_08232018_`subsample'_`group_id_selected'_seed_`set_seed'.tex, replace b(%10.5f) r2 ar2 pr2 compress booktabs page se margin star scalars(N_clust) mtitles((OLS) (IV)  (IV)) label coeflabels("Price ($P$)" "Energy Star ($D$)"  "Rebate (RebateXD)" "Elec Cost ($C$)") 
						
					}					
					
if `t_1_7'==1{   				
_eststo t_1_7_`group_id_selected'_seed_`set_seed', title("CLOGIT") : xi: clogit choice real_price elec_cost_county rebate_utility rebate_cfa s_estar i.pid_id ///
					i.type_id*i.income i.type_id*i.education i.type_id|fam_size  i.type_id|age i.type_id*i.political_id ///
					i.income|AV i.education|AV ageXAV fam_sizeXAV i.political_id|AV  ///
					i.county_es i.county_size i.brand_week_num, gr(hd_id) iterate(50) vce(cluster zipcode) nonest

cd $pathresults					
esttab t_1_7_`group_id_selected'_seed_`set_seed'  using Tables_EEGap_CL_stata_7_2008_2012_cty_paper_robust_08232018_`subsample'_`group_id_selected'_seed_`set_seed'.tex, replace b(%10.5f) r2 ar2 pr2 compress booktabs page se margin star scalars(N_clust) mtitles((OLS) (IV)  (IV)) label coeflabels("Price ($P$)" "Energy Star ($D$)"  "Rebate (RebateXD)" "Elec Cost ($C$)") 
						
					}	

if `t_1_8'==1{   				
_eststo t_1_8_`group_id_selected'_seed_`set_seed', title("CLOGIT") : xi: clogit choice real_price elec_cost_county rebate_utility rebate_cfa s_estar i.pid_id ///
					i.type_id*i.income i.type_id*i.education i.type_id|fam_size  i.type_id|age i.type_id*i.political_id ///
					i.income|AV i.education|AV ageXAV fam_sizeXAV i.political_id|AV  ///
					i.county_es i.county_type_2_3 i.county_size i.brand_week_num, gr(hd_id) iterate(75) vce(cluster zipcode) nonest

cd $pathresults					
esttab t_1_8_`group_id_selected'_seed_`set_seed'  using Tables_EEGap_CL_stata_8_2008_2012_cty_paper_robust_08232018_`subsample'_`group_id_selected'_seed_`set_seed'.tex, replace b(%10.5f) r2 ar2 pr2 compress booktabs page se margin star scalars(N_clust) mtitles((OLS) (IV)  (IV)) label coeflabels("Price ($P$)" "Energy Star ($D$)"  "Rebate (RebateXD)" "Elec Cost ($C$)") 
						
					}	
					
log close

 






